package me.send.business.service.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import me.send.business.base.Page;
import me.send.business.base.SendVariable;
import me.send.business.service.BaseService;
import me.send.business.service.bean.AdvertBean;
import me.send.business.service.bean.HomeBean;
import me.send.business.service.bean.OrderBean;
import me.send.business.service.bean.ReturnBean;
import me.send.business.service.mapper.AdvertMapper;
import me.send.business.service.mapper.OrdersMapper;
import me.send.business.service.pojo.Advert;
import me.send.framework.util.CacheHelper;
import me.send.framework.util.CacheUtil;
import me.send.framework.util.HelperUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class IndexService extends BaseService {

	// 实例化要用到的mapper
	// 广告的mapper 在首页上面加载 mapper 2015年6月2日10:24:54
	@Autowired
	private AdvertMapper advertMapper;

	// 设置 订单的分页 2015年6月3日09:23:38
	@Autowired
	private OrdersMapper ordersMapper;
	
	@Autowired
	private OpencityService opencityService;
	
	
	
	public Map<String, Object> validate(String phonenumber,String code) {
		Map<String, Object> map = new HashMap<String, Object>();
//		verificationMemCachedClient();
		map.put("data", "0");
//		if (CacheHelper.get(phonenumber)!=null) {
		CacheUtil cacheUtil = CacheUtil.getInstance();
		if (cacheUtil.get(phonenumber)!=null) {
			String codes = CacheHelper.get(phonenumber).toString();
			if (codes.equals(code)) {
				map.put("data", 1);
			}
		}
		return map;
	}
	
	/**
	 * 测试注册接口
	 */
	public void code(final String phonenumber) {
//		verificationMemCachedClient();
		// 放入手机号码 和 验证码
		Date regDate = new Date(180*1000);
		int x = (int) (Math.random() * 9000 + 1000);
		
		CacheHelper.replace(phonenumber, x+"", regDate);
	}
	
	
	
	
	

//	public Map<String, Object> home(HttpServletRequest request,
//			String devicenumber, Page page,Integer userid) {
//		Map<String, Object> map = new HashMap<String, Object>();
//		// 设置返回对象
//		map.put(SendVariable.COMMON_LIST, new ArrayList<Map<String, Object>>());
//		// 返回状态
//		map.put(SendVariable.COMM_ERROR, error_1001());
//		map.put(SendVariable.COMMON_ALL, new ArrayList<Map<String, Object>>());
//		// 验证请求的 2015年6月2日14:17:34
//		if (isDevicenumber(devicenumber)) {
//			List<Advert> all = advertMapper.list();
//			// 设置广告下面所有图片的链接
//			map.put(SendVariable.COMMON_ALL, all);
//			// 设置 下面的分页数据
//			if (page == null) { // 判断分页对象是否是null
//				page = new Page(); // 如果是null 给这个分页对象实例化一个
//			}
//			// 设置一个要传过去的参数 2015年6月3日09:27:17
//			if (page.getStart() + page.getSize() > Integer
//					.parseInt(getConfigurationValue(SendVariable.home_order_size)
//							.toString())) {
//				// 如果大于 list下面还是null
//			}else {
//				Map<String, Object> param = new HashMap<String, Object>();
//				// 增加参数  一个是page 分页  一个是 userid
//				param.put("page", page);
//				param.put("loginuserid", userid);
//				List<Map<String, Object>> ordersList = ordersMapper.list_page_all(param);
//				map.put(SendVariable.COMMON_LIST, ordersList);
//			}
//			// 返回状态
//			map.put(SendVariable.COMM_ERROR, error_1000());
//		} else {
//			map.put(SendVariable.COMM_ERROR, error_1002());
//		}
//		return map;
//	}
	
	
	/**
	 * 用户端首页接口
	 * @author maxmin
	 * @param devicenumber
	 * @param page
	 * @param userid
	 * @return
	 */
	public HomeBean index(String devicenumber, int startid,int size,Integer userid,String cityname){
		HomeBean hb = new HomeBean();
		try {
			if (opencityService.getCityIsOpenByName(cityname)==1) {
				Map<String, Object> p1 = new HashMap<String, Object>();
				p1.put("cityname", cityname);
				p1.put("currentDate", HelperUtil.getCurrentDate());
				hb.setAdverts(advertMapper.list(p1));
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("startid", startid);
				params.put("size", size);
				params.put("userid", userid);
				params.put("cityname", cityname);
				List<OrderBean> indexOrders = ordersMapper.indexOrders(params);
				for (OrderBean order : indexOrders) {
					String picpath = order.getPicpath();
					if (picpath!=null && !"".equals(picpath) && picpath.length()>0) {
						String[] paths = picpath.split(",");
						order.setPicpaths(paths);
					}
				}
				hb.setBrokerCount(ordersMapper.broker_count(cityname));
				hb.setCode(ReturnBean.SUCCESS);
				hb.setMessage(ReturnBean.SUCCESS_STR);
				hb.setDatas(indexOrders);
			}else {
				hb.setMessage("您所在的城市未开放");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return hb;
	}
	
	/**
	 * 获取订单列表
	 * @param devicenumber
	 * @param startid
	 * @param size
	 * @param userid
	 * @param cityname
	 * @return
	 * @author maxmin
	 * @date 2015年10月8日下午1:46:05
	 */
	public List<OrderBean> ordersIndex(String devicenumber, int startid,int size,Integer userid,String cityname){
		List<OrderBean> orders = null;
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("startid", startid);
			params.put("size", size);
			params.put("userid", userid);
			params.put("cityname", cityname);
			orders = ordersMapper.indexOrders(params);
			for (OrderBean order : orders) {
				String picpath = order.getPicpath();
				if (picpath!=null && !"".equals(picpath) && picpath.length()>0) {
					String[] paths = picpath.split(",");
					order.setPicpaths(paths);
				}
			}
			//oib.setBrokerCount(ordersMapper.broker_count());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return orders;
	}
	
	/**
	 * 获取广告列表
	 * @param devicenumber
	 * @param cityname
	 * @return
	 * @author maxmin
	 * @date 2015年10月8日下午1:38:19
	 */
	public AdvertBean advertList(String cityname){
		AdvertBean advertBean = new AdvertBean();
		try {
			Map<String, Object> p = new HashMap<String, Object>();
			p.put("cityname", cityname);
			p.put("currentDate", HelperUtil.getCurrentDate());
			List<Advert> list = advertMapper.list(p);
			advertBean.setAdverts(list);
			advertBean.setBrokerCount(ordersMapper.broker_count(cityname));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return advertBean;
	}
	
	/**
	 * 获取任务中的订单列表
	 * @param devicenumber
	 * @param startid
	 * @param size
	 * @param userid
	 * @param cityname
	 * @return
	 * @author maxmin
	 * @date 2015年10月8日下午2:00:41
	 */
	public List<OrderBean> ordersTasking(String devicenumber, int startid,int size,Integer userid,String cityname){
		List<OrderBean> orders = null;
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("startid", startid);
			params.put("size", size);
			params.put("userid", userid);
			params.put("cityname", cityname);
			orders = ordersMapper.tasking(params);
			for (OrderBean order : orders) {
				String picpath = order.getPicpath();
				if (picpath!=null && !"".equals(picpath) && picpath.length()>0) {
					String[] paths = picpath.split(",");
					order.setPicpaths(paths);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return orders;
	}
	
	public HomeBean tasking(String devicenumber, int startid,int size,Integer userid,String cityname){
		HomeBean hb = new HomeBean();
		try {
			Map<String, Object> p1 = new HashMap<String, Object>();
			p1.put("cityname", cityname);
			p1.put("currentDate", HelperUtil.getCurrentDate());
			hb.setAdverts(advertMapper.list(p1));
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("startid", startid);
			params.put("size", size);
			params.put("userid", userid);
			params.put("cityname", cityname);
			List<OrderBean> indexOrders = ordersMapper.tasking(params);
			for (OrderBean order : indexOrders) {
				String picpath = order.getPicpath();
				if (picpath!=null && !"".equals(picpath) && picpath.length()>0) {
					String[] paths = picpath.split(",");
					order.setPicpaths(paths);
				}
			}
			hb.setBrokerCount(ordersMapper.broker_count(cityname));
			hb.setCode(ReturnBean.SUCCESS);
			hb.setMessage(ReturnBean.SUCCESS_STR);
			hb.setDatas(indexOrders);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return hb;
	}

	public Map<String, Object> home2(HttpServletRequest request,
			String devicenumber, Page page,int userid) {
		Map<String, Object> map = new HashMap<String, Object>();
		// 设置返回对象
		map.put(SendVariable.COMMON_LIST, new ArrayList<Map<String, Object>>());
		// 返回状态
		map.put(SendVariable.COMM_ERROR, error_1001());
		// 验证请求的 2015年6月2日14:17:34
		if (isDevicenumber(devicenumber)) {
			// 设置 下面的分页数据
			if (page == null) { // 判断分页对象是否是null
				page = new Page(); // 如果是null 给这个分页对象实例化一个
			}
			// 设置一个要传过去的参数 2015年6月3日09:27:17
			// 判断 这个userid是否是0
			if (userid>0) {
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("page", page);
				param.put("userid", userid);
				List<Map<String, Object>> ordersList = ordersMapper.list_page2(param);
				map.put(SendVariable.COMMON_LIST, ordersList);
			}else {
				map.put(SendVariable.COMM_ERROR, error_1006());
			}
			// 返回状态
			map.put(SendVariable.COMM_ERROR, error_1000());
		} else {
			map.put(SendVariable.COMM_ERROR, error_1002());
		}
		return map;
	}

}
